package com.programming.forum.common;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 22479
 * Date: 2023-11-04
 * Time: 16:53
 */

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 自定义返回结果
 * @param <T>
 */
public class AppResult<T> {

    //状态码
    @JsonInclude(JsonInclude.Include.ALWAYS) //不论如何情况都参与JSON序列化
    private int code;
    //描述信息
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    //具体的数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    /**
     * 构造方法
     * @param code
     * @param message
     */
    public AppResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 操作成功
     * @return
     */
    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static AppResult success(String message) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    public static <T> AppResult<T> success(T date) {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),date);
    }

    public static <T> AppResult<T> success(String message, T date) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message,date);
    }

    /**
     * 操作失败
     * @param
     */
    public static  AppResult failed() {
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static  AppResult failed(String message) {
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }

    //参数就是ResultCode
    public static  AppResult failed(ResultCode resultCode) {
        return new AppResult(resultCode.getCode(), resultCode.getMessage());
    }





    public void setCode(int code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
}
